package com.mc.moring.widge;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mc.moring.R;
import com.mc.moring.bean.BankAccountListBean;
import com.mc.moring.util.DensityUtil;
import com.mc.moring.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class SingleSelectBankDialogView extends PopupWindow {

    View view;
    Context context;
    private PopupWindow popupWindow;
    private RecyclerView recycler_view;
    private TextView tv_confirm;

    private BankAdapter adapter;

    private int selectLast = 0;
    private boolean haveTarget = true;
    private List<BankAccountListBean.DataBean.AccountsBean> list = new ArrayList<>();

    private static JDialogCallback callback;

    public SingleSelectBankDialogView(Context context) {
        this.context = context;
    }

    public void setList(List<BankAccountListBean.DataBean.AccountsBean> list) {
        this.list = list;
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    public void showSingleSelectDialog(Context context, View view, JDialogCallback onClickListener) {
        this.context = context;
        callback = onClickListener;

        SharedPreferences mySharedPreferences = context.getSharedPreferences("setSelectPhone", Context.MODE_PRIVATE);
        String position = mySharedPreferences.getString("setSelectPosition","");
        selectLast = Integer.parseInt(position);

        View popuView = View.inflate(context, R.layout.pop_select_bank, null);
        popupWindow = new PopupWindow(popuView, DensityUtil.getScreenWidth((Activity) context), ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {

                backgroundAlpha();
                callback.cancel(view);
            }
        });
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);

        recycler_view = popuView.findViewById(R.id.recycler_view);
        tv_confirm = popuView.findViewById(R.id.tv_confirm);
        recycler_view.setLayoutManager(new LinearLayoutManager(context));
        if (adapter == null) {
            adapter = new BankAdapter();
        }
        recycler_view.setAdapter(adapter);

        ImageView iv_close = popuView.findViewById(R.id.iv_close);
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                callback.cancel(view);
            }
        });
        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClick != null){
                    itemClick.onItemClick(v, adapter.selectPosition);
                    callback.cancel(view);
                }
                popupWindow.dismiss();
            }
        });
        backgroundAlpha1();
    }

    private void backgroundAlpha1() {//透明函数
        WindowManager.LayoutParams lp = ((Activity)context).getWindow().getAttributes();
        lp.alpha = (float) 0.5;
        ((Activity)context).getWindow().setAttributes(lp);
    }

    class BankAdapter extends RecyclerView.Adapter<ViewHolder> {

        public int selectPosition = 0;

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(context)
                    .inflate(R.layout.adapter_select_bank, parent, false);
            return new ViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
            BankAccountListBean.DataBean.AccountsBean item = list.get(position);
            holder.tv_account.setText(item.getBankName()+"("+StringUtils.keepLastFourDigits(item.getAccountNumber())+")");

            selectPosition = selectLast;

            Log.e("----selectPosition",String.valueOf(selectPosition));
            holder.ll_item.setSelected(position == selectPosition);
            holder.ll_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectPosition = position;
                    selectLast = selectPosition;
                    SharedPreferences sharedPreferences = context.getSharedPreferences("setSelectPhone", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("setSelectPhone",StringUtils.keepLastFourDigits(item.getAccountNumber()));
                    editor.putString("setSelectBank",item.getBankName());
                    editor.putString("setSelectPosition",String.valueOf(position));
                    editor.apply();
                    adapter.notifyDataSetChanged();
                }
            });
        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }

    onItemClickListener itemClick;

    public void setItemClick(onItemClickListener itemClick) {
        this.itemClick = itemClick;
    }

    public interface onItemClickListener{
        void onItemClick(View view, int position);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        LinearLayout ll_item;
        TextView tv_account;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ll_item = itemView.findViewById(R.id.ll_item);
            tv_account = itemView.findViewById(R.id.tv_account);
        }
    }

    private void backgroundAlpha() {//透明函数
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = (float) 1.0;
        ((Activity) context).getWindow().setAttributes(lp);
    }

    public void dismiss() {
        super.dismiss();
    }
}
